package com.gtfrank.web.servlet.day3;

import com.gtfrank.web.servlet.pojo.User;
import com.gtfrank.web.servlet.service.IUserService;
import com.gtfrank.web.servlet.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ProjectName: sudacoursetrainging
 * @ClassName: UserToModifyServlet
 * @PackageName:com.gtfrank.web.servlet.day3
 * @Description: 用户去修改，准备用户信息
 * @Author: frank
 * @Date: 2021/4/29 14:50
 * @Version v1.0
 */
@WebServlet(value = "/user/UserToModifyServlet")
public class UserToModifyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        IUserService userService = new UserServiceImpl();
        String id = req.getParameter("userid");
        int userId = Integer.parseInt(id);
        User user = userService.getUserById(userId);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html lang=\"en\">");
        out.println("<head>");
        out.println("	<meta charset=\"UTF-8\">");
        out.println("");
        out.println("		<title>修改</title>");
        out.println("		<style>		");
        out.println("			table{");
        out.println("				position:absolute ;");
        out.println("				left:35%;");
        out.println("				border:1px solid green;");
        out.println("			}");
        out.println("			h2{");
        out.println("				position:relative  ;");
        out.println("				left:45%;");
        out.println("			}");
        out.println("			.input1{");
        out.println("				width:350px;");
        out.println("			}");
        out.println("			.td1{");
        out.println("				text-align :right;");
        out.println("			");
        out.println("			}");
        out.println("		</style>");
        out.println("	</head>");
        out.println("");
        out.println("	<body>");
        out.println("<h2>用户管理---修改</h2>");
        out.println("<hr>");

        out.println("<form id=\"form\" method=\"post\" action=\"/javaeeproject/user/UpdateUserServlet\">");
        out.println("	<table  width=\"650px\" >");
        out.println("		<tr><td class=\"td1\">用户ID</td><td><input id=\"userid\" class=\"input1\" type=\"text\" name=\"userid\" value="+user.getId()+" readOnly= \"readOnly\"/></td></tr>");
        out.println("		<tr><td class=\"td1\">用户名</td><td><input id=\"name\" class=\"input1\" type=\"text\" name=\"userName\" value="+user.getName()+"></td></tr>");
        out.println("		<tr><td class=\"td1\">密码</td><td><input id=\"password1\"  class=\"input1\" type=\"password\" name=\"password\" value="+user.getPassword()+"/></td></tr>");
        out.println("		<tr><td  colspan=\"2\" align=\"center\"><input  type=\"submit\" value=\"修改\"/>");
        out.println("	</table>");

        out.println("			</form>");
        out.println("	</body>");

        out.println("</html>");
        out.flush();
        out.close();

    }
}
